package com.kaiyiweitong.tongxing.activity.my;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.activity.my.my_share.MyFansActivity;
import com.kaiyiweitong.tongxing.activity.my.my_share.MySubordinateActivity;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.bean.MyShareResData;
import com.kaiyiweitong.tongxing.constant.HttpConstant;
import com.kaiyiweitong.tongxing.constant.LogicConstant;
import com.kaiyiweitong.tongxing.util.CommonUtil;
import com.kaiyiweitong.tongxing.util.HttpUtil;
import com.kaiyiweitong.tongxing.util.SpUtils;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/17 0017.
 */

public class MyShareActivity extends BaseActivity {
    @Bind(R.id.share_icon)
    ImageView mShareIcon;
    @Bind(R.id.my_sub_num)
    TextView mMySubNum;
    @Bind(R.id.my_fans_num)
    TextView mMyFansNum;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_share);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("id", SpUtils.getString(this, LogicConstant.USER_ID));
        HttpUtil.get(HttpConstant.USER_QR_CODE, map, this, new HttpUtil.MyCallBack() {
            @Override
            public void onResult(String result) {
                if (result != null && !result.equals("")) {
                    MyShareResData data = HttpUtil.checkResponse(MyShareActivity.this, result, MyShareResData.class);
                    if (data != null) {
                        showData(data);
                    }
                }
            }
        });
    }

    private void showData(MyShareResData data) {
        Bitmap bitmap = generateBitmap(data.getCode(), CommonUtil.dip2px(this, 154), CommonUtil.dip2px(this, 154));
        mShareIcon.setImageBitmap(bitmap);
        mMySubNum.setText(String.valueOf(data.getSub()));
        mMyFansNum.setText(String.valueOf(data.getFans()));

    }

    @OnClick({R.id.rl_my_sub, R.id.my_fans})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_my_sub:
                go2Page(MySubordinateActivity.class);
                break;
            case R.id.my_fans:
                go2Page(MyFansActivity.class);
                break;
        }
    }

    private void go2Page(Class clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }


    private Bitmap generateBitmap(String content, int width, int height) {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
//        Map<EncodeHintType, String> hints = new HashMap<>();
        Hashtable<EncodeHintType, String> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (encode.get(j, i)) {
                        pixels[i * width + j] = 0x00000000;
                    } else {
                        pixels[i * width + j] = 0xffffffff;
                    }
                }
            }
            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

}
